package com.fjwt.gz.proxy.ctrl.activity;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.components.oss.model.OssFileConfig;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.ActivityChannelDTO;
import com.fjwt.gz.db.entity.ActivityChannelEntity;
import com.fjwt.gz.db.vo.ActivityChannelVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.ActivityChannelService;
import com.fjwt.gz.service.impl.AgentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动渠道管理 Controller
 */
@RestController
@Slf4j
@RequestMapping("/api/activity/channel")
public class ActivityChannelController extends CommonCtrl{

    @Autowired
    private ActivityChannelService activityChannelService;

    @Autowired
    private AgentInfoService agentInfoService;

    /**
     * 获取商户渠道列表
     *
     * @return
     */
    @PostMapping("/getChannelList")
    public ApiRes getChannelList() {
        ActivityChannelDTO activityChannelDTO = getObject(ActivityChannelDTO.class);
        activityChannelDTO.setAgentNoList(agentInfoService.queryAllSubAgentNo(getCurrentAgentNo()));
        log.info("proxy端获取商户渠道列表，请求地址：/api/activity/channel/getChannelList，请求参数：{}", JSONUtil.toJsonStr(activityChannelDTO));
        IPage<ActivityChannelVO> page = activityChannelService.getChannelList(getIPage(), activityChannelDTO);
        log.info("proxy端获取商户渠道列表，请求地址：/api/activity/channel/getChannelList，返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     * 获取自建渠道码列表
     * @return
     */
    @PostMapping("/getSelfBuildChannelList")
    public ApiRes getSelfBuildChannelList() {
        ActivityChannelDTO activityChannelDTO = getObject(ActivityChannelDTO.class);
        activityChannelDTO.setAgentNoList(agentInfoService.queryAllSubAgentNo(getCurrentAgentNo()));
        log.info("proxy端获取自建渠道列表，请求地址：/api/activity/channel/getSelfBuildChannelList，请求参数：{}", JSONUtil.toJsonStr(activityChannelDTO));
        IPage<ActivityChannelVO> page = activityChannelService.getSelfBuildChannelList(getIPage(), activityChannelDTO);
        log.info("proxy端获取自建渠道列表，请求地址：/api/activity/channel/getSelfBuildChannelList，返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     * 获取自建渠道码详情
     * @return
     */
    @PostMapping("/getSelfBuildChannelDetail")
    public ApiRes getSelfBuildChannelDetail() {
        ActivityChannelDTO activityChannelDTO = getObject(ActivityChannelDTO.class);
        log.info("proxy端获取自建渠道详情，请求地址：/api/activity/channel/getSelfBuildChannelDetail，请求参数：{}", JSONUtil.toJsonStr(activityChannelDTO));
        ActivityChannelVO channelVO = activityChannelService.getSelfBuildChannelDetail(activityChannelDTO);
        log.info("proxy端获取自建渠道详情，请求地址：/api/activity/channel/getSelfBuildChannelDetail，返回结果：{}", JSONUtil.toJsonStr(channelVO));
        return ApiRes.ok(channelVO);
    }


    /**
     * 新增单个渠道码
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_CHANNEL_INFO_ADD', 'ENT_CHANNEL_EMPLOYEES_ADD', 'ENT_CHANNEL_OUTLETS_ADD')")
    @PostMapping("/addOne")
    public ApiRes addOne() {
        ActivityChannelDTO activityChannelDTO = getObject(ActivityChannelDTO.class);
        log.info("proxy端新增单个渠道码，请求地址：/api/activity/channel/addOne，请求参数：{}", JSONUtil.toJsonStr(activityChannelDTO));
        JSONObject object = activityChannelService.addOne(activityChannelDTO);
        log.info("proxy端新增单个渠道码，请求地址：/api/activity/channel/addOne，返回结果：{}", JSONUtil.toJsonStr(object));
        return ApiRes.ok(object);
    }

    /**
     * 批量新增渠道码
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_CHANNEL_INFO_ADD', 'ENT_CHANNEL_EMPLOYEES_ADD', 'ENT_CHANNEL_OUTLETS_ADD')")
    @PostMapping("/add")
    public ApiRes addBatch() {
        ActivityChannelDTO activityChannelDTO = getObject(ActivityChannelDTO.class);
        log.info("proxy端新增活动渠道码，请求地址：/api/activity/channel/add，请求参数：{}", JSONUtil.toJsonStr(activityChannelDTO));
        JSONObject object = activityChannelService.addByBatch(activityChannelDTO, OssFileConfig.BIZ_TYPE.UPLOAD);
        log.info("proxy端新增活动渠道码，请求地址：/api/activity/channel/add，返回结果：{}", object);
        return ApiRes.ok(object);
    }

    /**
     * 导出渠道码
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_CHANNEL_INFO_EXPT', 'ENT_CHANNEL_EMPLOYEES_EXPT', 'ENT_CHANNEL_OUTLETS_EXPT')")
    @PostMapping("/export")
    public ApiRes export() {
        ActivityChannelDTO activityChannelDTO = getObject(ActivityChannelDTO.class);

        log.info("proxy端导出活动渠道码，请求地址：/api/activity/channel/export，请求参数：{}", JSONUtil.toJsonStr(activityChannelDTO));
        // 查询渠道码
        activityChannelDTO.setAgentNoList(agentInfoService.queryAllSubAgentNo(getCurrentAgentNo()));
        List<ActivityChannelVO> list = activityChannelService.allList(activityChannelDTO);
        List<ActivityChannelVO> vos = List.copyOf(list);
        log.info("proxy端导出活动渠道码，请求地址：/api/activity/channel/export，返回数据：渠道码数据：{}", JSONUtil.toJsonStr(list));
        return ApiRes.ok(vos);
    }

    /**
     * 开关距离限制
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_CHANNEL_MCH_DISTANCE')")
    @PostMapping("/distance/limit")
    public ApiRes channelDistanceLimit() {
        Long activityChannelId = getValLong("activityChannelId");
        Integer isDistanceLimit = getValInteger("isDistanceLimit");
        Float distanceLimit = null;
        if (isDistanceLimit == 1) {
            distanceLimit = getValRequired("distanceLimit", Float.class);
        }
        log.info("proxy端开关距离限制，请求地址：/api/activity/channel/distance/limit，请求参数：activityChannelId：{}，isDistanceLimit：{}，distanceLimit：{}", activityChannelId, isDistanceLimit, distanceLimit);
        activityChannelService.updateDistanceLimit(activityChannelId, isDistanceLimit, distanceLimit);
        return ApiRes.ok();
    }

    /**
     * 删除渠道
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_CHANNEL_INFO_EDIT', 'ENT_CHANNEL_EMPLOYEES_EDIT', 'ENT_CHANNEL_OUTLETS_EDIT')")
    @PostMapping("/channelDelete")
    public ApiRes channelDelete() {
        Long activityChannelId = getValLong("activityChannelId");
        Integer isDeleted = getValInteger("isDeleted");
        log.info("proxy端删除活动渠道码，请求地址：/api/activity/channel/channelDelete，请求参数：activityChannelId：{}，isDeleted：{}", activityChannelId, isDeleted);
        activityChannelService.deleteChannelById(activityChannelId, isDeleted);
        return ApiRes.ok();
    }

    /**
     * 新增单个自建渠道码
     * @return
     */
    @PostMapping("/addOneSelfBuild")
    public ApiRes addOneSelfBuild() {
        ActivityChannelDTO activityChannelDTO = getObject(ActivityChannelDTO.class);
        log.info("proxy端新增单个自建渠道码，请求地址：/api/activity/channel/addOneSelfBuild，请求参数：{}", JSONUtil.toJsonStr(activityChannelDTO));
        ActivityChannelEntity channelEntity = activityChannelService.addOneSelfBuild(activityChannelDTO);
        log.info("proxy端新增单个自建渠道码，请求地址：/api/activity/channel/addOneSelfBuild，返回结果：{}", JSONUtil.toJsonStr(channelEntity));
        return ApiRes.ok(channelEntity);
    }

    /**
     * 批量导入自建渠道码
     * @return
     */
    @PostMapping("/importSelfBuildFile")
    public ApiRes importSelfBuildFile() {
        ActivityChannelDTO activityChannelDTO = getObject(ActivityChannelDTO.class);
        log.info("proxy端批量导入自建渠道码，请求地址：/api/activity/channel/importSelfBuildFile，请求参数：{}", JSONUtil.toJsonStr(activityChannelDTO));
        JSONObject object = activityChannelService.importSelfBuildFile(activityChannelDTO);
        log.info("proxy端批量导入自建渠道码，请求地址：/api/activity/channel/importSelfBuildFile，返回结果：{}", object);
        return ApiRes.ok(object);
    }

    /**
     * 修改自建渠道码
     * @return
     */
    @PostMapping("/updateSelfBuildChannel")
    public ApiRes updateSelfBuildChannel() {
        ActivityChannelDTO activityChannelDTO = getObject(ActivityChannelDTO.class);
        log.info("proxy端修改自建渠道码，请求地址：/api/activity/channel/updateSelfBuildChannel，请求参数：{}", JSONUtil.toJsonStr(activityChannelDTO));
        activityChannelService.updateSelfBuildChannel(activityChannelDTO);
        return ApiRes.ok();
    }

    /**
     * 停用启用自建渠道码
     * @return
     */
    @PostMapping("/updateSelfBuildChannelStatus")
    public ApiRes updateSelfBuildChannelStatus() {
        Long activityChannelId = getValLong("activityChannelId");
        Integer isDeleted = getValInteger("isDeleted");
        log.info("proxy端停用启用渠道码，请求地址：/api/activity/channel/updateChannelStatus，请求参数：{}", JSONUtil.toJsonStr(activityChannelId));
        activityChannelService.updateSelfBuildChannelStatus(activityChannelId, isDeleted);
        return ApiRes.ok();
    }

    /**
     * 导出自建渠道码
     * @return
     */
    @PostMapping("/exportSelfBuildChannel")
    public ApiRes exportSelfBuildChannel() {
        ActivityChannelDTO activityChannelDTO = getObject(ActivityChannelDTO.class);
        log.info("proxy端导自建渠道码，请求地址：/api/activity/channel/exportSelfBuildChannel，请求参数：{}", JSONUtil.toJsonStr(activityChannelDTO));
        activityChannelDTO.setAgentNoList(agentInfoService.queryAllSubAgentNo(getCurrentAgentNo()));
        List<ActivityChannelVO> list = activityChannelService.exportSelfBuildChannel(activityChannelDTO);
        log.info("proxy端导自建渠道码，请求地址：/api/activity/channel/exportSelfBuildChannel，返回结果：{}", JSONUtil.toJsonStr(list));
        return ApiRes.ok(list);
    }

    /**
     * 导出自建渠道码列表
     * @return
     */
    @PostMapping("/exportSelfBuildChannelList")
    public ApiRes exportSelfBuildChannelList() {
        ActivityChannelDTO activityChannelDTO = getObject(ActivityChannelDTO.class);
        log.info("proxy端导自建渠道码列表，请求地址：/api/activity/channel/exportSelfBuildChannelList，请求参数：{}", JSONUtil.toJsonStr(activityChannelDTO));
        activityChannelDTO.setAgentNoList(agentInfoService.queryAllSubAgentNo(getCurrentAgentNo()));
        String url = activityChannelService.exportSelfBuildChannelList(activityChannelDTO);
        log.info("proxy端导自建渠道码列表，请求地址：/api/activity/channel/exportSelfBuildChannelList，返回结果：{}", JSONUtil.toJsonStr(url));
        return ApiRes.ok(url);
    }


}
